<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Widget_Index extends Controller_Widget_Base
{
    public function action_index()
    {    
        $source_id = $this->request->param('id') ? $this->request->param('id') : 0;
        $page       = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $content = ORM::factory('shop')->get_widget_view('index/index_tpl');

        $product = ORM::factory('product')->get_product($source_id);
        if (!$product || !$product->model_id) {
            $content = ORM::factory('shop')->get_widget_view('index/no_reviews_tpl');    
        } else {
            $model   = ORM::factory('model', $product->model_id);
            if (!$model->count_reviews) {
                $content = ORM::factory('shop')->get_widget_view('index/no_reviews_tpl');    
            } else {
                $content->reviews = Model_Review::get_reviews($product, $page);
                $content->product = $product;
                $content->model   = $model;
                $content->count   = $model->count_reviews;
                $content->pagination = array(
                    'page' => $page,
                    'per_page' => ORM::factory('review')->get_reviews_per_page(),
                );
            }
        }
    
        $this->print_template($content); 
    }
    
    public function action_count()
    {
        $source_id = $this->request->param('id') ? $this->request->param('id') : 0;
        $product = ORM::factory('product')->get_product($source_id);
        $count   = ORM::factory('review')->get_reviews($product, null, true);
        die('recensee_inline_widget.set_count(' . json_encode($count) . ');');   
    }
} 
